<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>3_2_3-计算属性的赋值</title>
</head>

<body>


  <!--载入vue.js-->
  <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>

  <div id="Application">
    <h1>{{count}}</h1>
    <h1>{{type}}</h1>
    <button @click="add">Add</button>
  </div>

  <script>
    const App = {
      data() {
        return {
          count: 0,
        };
      },
      computed: {
        type: {
          //实现计算属性的get方法，用来取值
          get() {
            return this.count > 11 ? "大" : "小";
          },

          //实现计算属性的set方法，用来设置值
          set(newValue) {
            if (newValue == "大") {
              this.count = 11;
            } else {
              this.count = 0;
            }
          },
        },
      },

      methods: {
        add() {
          this.count++;
        },
      },
    };

    //创建组件并获取组件实例
    let instance = Vue.createApp(App).mount("#Application");
    //像访问普通属性一样访问计算属性
    //初始值为0
    console.log(instance.count);
    //初始状态为小
    console.log(instance.type);

    //对计算属性进行修改
    instance.type = "大";

    //打印结果为11
    console.log(instance.count);
  </script>
</body>

</html>